//
//  MainViewController.m
//  weibo
//
//  Created by qiuming on 15/12/21.
//  Copyright © 2015年 qiuming. All rights reserved.
//

#import "MainViewController.h"
#import "HomeViewController.h"
#import "MessageViewController.h"
#import "DiscoverViewController.h"
#import "ProfileViewController.h"
#import "Extensions.h"

@interface MainViewController ()

@property (strong, nonatomic) UIButton *composeBtn;
@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 隐藏导航条上的分割线
    self.tabBar.shadowImage = [[UIImage alloc]init];
    self.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar_background"];
    // 添加子控制器
    [self addChildViewControllers];

}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    // 添加撰写按钮
    [self setComposeBtnController];
}

// MARK: - 添加撰写按钮
- (void)setComposeBtnController {
    
    CGRect rect = self.tabBar.bounds;
    CGFloat w = rect.size.width / self.childViewControllers.count;
    self.composeBtn.frame= CGRectInset(rect, 2 * w, 0);
}

// MARK: - 添加子控制器
- (void)addChildViewControllers {
    
    // 首页
    [self addChildViewControllers:[[HomeViewController alloc]init] title:@"首页" imageName:@"tabbar_home"];
    // 消息
    [self addChildViewControllers:[[MessageViewController alloc]init] title:@"消息" imageName:@"tabbar_message_center"];
    // 撰写
    [self addChildViewController:[[UIViewController alloc]init]];
    // 发现
    [self addChildViewControllers:[[DiscoverViewController alloc]init] title:@"发现" imageName:@"tabbar_discover"];
    // 我
    [self addChildViewControllers:[[ProfileViewController alloc]init] title:@"我" imageName:@"tabbar_profile"];
    
}

- (void)addChildViewControllers:(UIViewController *)vc title:(NSString *)title imageName:(NSString *)imageName
{
    vc.title = title;
    // 设置字体大小颜色
//    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];
    [vc.tabBarItem setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12]} forState:UIControlStateNormal];
    
    vc.tabBarItem.image = [UIImage imageNamed:imageName];
    
    NSString *selectImageName = [NSString stringWithFormat:@"%@_selected", imageName];
    // 解决图片蒙板问题
    UIImage *selectImage = [[UIImage imageNamed:selectImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = selectImage;
    
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    
    [self addChildViewController:nav];
}

// MARK: - 添加撰写按钮
- (void)composeBtnClick {
    NSLog(@"添加撰写按钮内容");
}

// MARK: - 懒加载
- (UIButton *)composeBtn {
    if (_composeBtn == nil) {
        
        _composeBtn = [UIButton setBtnWithImageName:@"tabbar_compose_icon_add" BackgroundImageName:@"tabbar_compose_button"];
        
        [self.tabBar addSubview:_composeBtn];
        
        [_composeBtn addTarget:self action:@selector(composeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _composeBtn;
}


@end
